
package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.SalaDao;
import edu.upc.clase.demo.entity.Sala;
import java.util.List;
import javax.sql.DataSource;
import org.codehaus.jackson.map.BeanProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author David
 */
@Repository
public class SalaDaoImpl extends SimpleJdbcDaoSupport implements SalaDao {
    
    private static Logger log = LoggerFactory.getLogger(UsuarioDaoImpl.class);
    
    @Autowired
    public SalaDaoImpl(DataSource dataSource) {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }

    @Override
    public Integer insertar(Sala sala) {
        getJdbcTemplate().update(
                "Insert Into Sala (nosala,nolocal,txdireccion,qtcapacidadmaxima,txcaracteristica,sscostoporhora) values ( ?, ?, ?, ?, ?, ?)",
                 sala.getNosala(), sala.getNolocal(), sala.getTxdireccion(), sala.getQtcapacidadmaxima(), sala.getTxcaracteristica(), sala.getSscostoporhora());
        return getSimpleJdbcTemplate().queryForInt("select last_insert_id()");
    }

    @Override
    public void actualizar(Sala sala) {
        getJdbcTemplate().update(
                "Update Sala Set nosala=?, nolocal=?, txdireccion=?, qtcapacidadmaxima=?, txcaracteristica=?, sscostoporhora=? Where cosala=?",
                sala.getNosala(), sala.getNolocal(), sala.getTxdireccion(), sala.getQtcapacidadmaxima(), sala.getTxcaracteristica(), sala.getSscostoporhora(), sala.getCosala());
    }

    @Override
    public void eliminar(Sala sala) {
        getSimpleJdbcTemplate().update(
                "Delete From Sala Where cosala=?", sala.getCosala());
    }

    @Override
    public List<Sala> buscarTodos() {
        return getSimpleJdbcTemplate().query(
                "Select cosala,nosala,nolocal,txdireccion,qtcapacidadmaxima,txcaracteristica,sscostoporhora From Sala", 
                new BeanPropertyRowMapper<Sala>(Sala.class));
    }

    @Override
    public Sala buscar(Integer cosala) {
        try {
            return getSimpleJdbcTemplate().queryForObject(
                    "Select cosala,nosala,nolocal,txdireccion,qtcapacidadmaxima,txcaracteristica,sscostoporhora from Sala Where cosala=?", 
                    new BeanPropertyRowMapper<Sala>(Sala.class), cosala);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
         
    @Override
    public Sala buscarNombreSalaxLocal(String nolocal, String nosala) {
        try {
            return getSimpleJdbcTemplate().queryForObject(
                    "Select cosala,nosala,nolocal,txdireccion,qtcapacidadmaxima,txcaracteristica,sscostoporhora From Sala Where nolocal=? and nosala=?", 
                    new BeanPropertyRowMapper<Sala>(Sala.class), nolocal, nosala);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

}
